//
//  ErrorCodePlistOption.m
//  O2O
//
//  Created by myqu on 14-8-20.
//  Copyright (c) 2014年 3TI. All rights reserved.
//

#import "ErrorCodePlistOption.h"

@implementation ErrorCodePlistOption
{
    NSString* plistFilePath; //  存储错误码文件的路径
    NSString* plistFileName; //  存储错误码文件的名称
}

static ErrorCodePlistOption *_instance = nil;

+(ErrorCodePlistOption *)shareInstance
{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[ErrorCodePlistOption alloc] init];
        
    });
    return _instance;
}

-(id)init
{
    if([super init])
    {
        plistFileName = @"ErrorCode-Info.plist";
        plistFilePath = [[NSBundle mainBundle]pathForResource:@"ErrorCode-Info" ofType:@"plist"];
        //Users/myqu/Library/Application Support/iPhone Simulator/7.1/Applications/73BBEF26-126B-41C9-B139-BA1FA067478E/ZJWR.app/ErrorCode-Info.plist
        
        
//        //获取应用程序沙盒的Documents目录
//        NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
//        NSString *path = [paths objectAtIndex:0];
//        //得到完整的文件名
//        plistFilePath=[path stringByAppendingPathComponent:plistFileName];
        
    }
    return self;
}

-(NSString *)getErrorMsgByCode:(NSString *)code
{
    NSMutableDictionary* data = [[NSMutableDictionary alloc]initWithContentsOfFile:plistFilePath];
    NSString *errMsg = [data objectForKey:code];
    
    
    return errMsg;
}

-(BOOL)updateErrorInfo:(NSDictionary *)dic
{
    NSMutableDictionary* data = [[NSMutableDictionary alloc]initWithContentsOfFile:plistFilePath];
    if(data == nil)
        data = [NSMutableDictionary dictionary];
    [data setDictionary:dic];
    
    BOOL ret = [data writeToFile:plistFilePath atomically:YES];
    
    //检查是否写入
    NSMutableDictionary *writeData=[[NSMutableDictionary alloc]initWithContentsOfFile:plistFilePath];
    NSLog(@"write data is :%@",writeData);
    
    return ret;
}

@end
